package com.ruoyi.project.system.coinrecord.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.coinrecord.domain.HaoqiCoinRecord;
import com.ruoyi.project.system.coinrecord.dto.HaoqiCoinRecordDto;
import com.ruoyi.project.system.coinrecord.service.IHaoqiCoinRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.order.refuse.domain.OrderRefuse;

/**
 * 好奇币记录Controller
 * 
 * @author f_baisi
 * @date 2020-06-29
 */
@Controller
@RequestMapping("/system/coinrecord")
public class HaoqiCoinRecordController extends BaseController
{
    private String prefix = "system/coinrecord";

    @Autowired
    private IHaoqiCoinRecordService haoqiCoinRecordService;

    @Autowired
    private IMemberService memberService;

    @RequiresPermissions("system:coinrecord:view")
    @GetMapping()
    public String coinrecord()
    {
        return prefix + "/coinrecord";
    }

    /**
     * 查询好奇币记录列表
     */
    @RequiresPermissions("system:coinrecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HaoqiCoinRecord haoqiCoinRecord)
    {
        startPage();
        haoqiCoinRecord.setType(1);
        List<HaoqiCoinRecordDto> list = haoqiCoinRecordService.selectHaoqiCoinRecordList(haoqiCoinRecord);
        return getDataTable(list);
    }

    /**
     * 导出好奇币记录列表
     */
//    @RequiresPermissions("system:coinrecord:export")
//    @Log(title = "好奇币记录", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(HaoqiCoinRecord haoqiCoinRecord)
//    {
//        List<HaoqiCoinRecord> list = haoqiCoinRecordService.selectHaoqiCoinRecordList(haoqiCoinRecord);
//        ExcelUtil<HaoqiCoinRecord> util = new ExcelUtil<HaoqiCoinRecord>(HaoqiCoinRecord.class);
//        return util.exportExcel(list, "coinrecord");
//    }

    /**
     * 新增好奇币记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存好奇币记录
     */
    @RequiresPermissions("system:coinrecord:add")
    @Log(title = "好奇币记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HaoqiCoinRecord haoqiCoinRecord)
    {
        return toAjax(haoqiCoinRecordService.insertHaoqiCoinRecord(haoqiCoinRecord));
    }

    /**
     * 修改好奇币记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        HaoqiCoinRecord haoqiCoinRecord = haoqiCoinRecordService.selectHaoqiCoinRecordById(id);
        mmap.put("haoqiCoinRecord", haoqiCoinRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存好奇币记录
     */
    @RequiresPermissions("system:coinrecord:edit")
    @Log(title = "好奇币记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HaoqiCoinRecord haoqiCoinRecord)
    {
        return toAjax(haoqiCoinRecordService.updateHaoqiCoinRecord(haoqiCoinRecord));
    }

    /**
     * 删除好奇币记录
     */
    @RequiresPermissions("system:coinrecord:remove")
    @Log(title = "好奇币记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(haoqiCoinRecordService.deleteHaoqiCoinRecordByIds(ids));
    }


    /**
     * 提现完成
     * @param id
     * @return
     */
    @PostMapping("/yes/{id}")
    @Transactional
    @ResponseBody
    public AjaxResult refuse(@PathVariable String id){
        HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
        haoqiCoinRecord.setId(id);
        haoqiCoinRecord.setStatus(1);
        this.haoqiCoinRecordService.updateHaoqiCoinRecord(haoqiCoinRecord);
        return AjaxResult.success();
    }
    /**
     * 提现完成
     * @param id
     * @return
     */
    @PostMapping("/no/{id}")
    @Transactional
    @ResponseBody
    public AjaxResult noRefuse(@PathVariable String id){
        HaoqiCoinRecord oldRec = this.haoqiCoinRecordService.selectHaoqiCoinRecordById(id);
        HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
        haoqiCoinRecord.setId(id);
        haoqiCoinRecord.setStatus(2);
        this.haoqiCoinRecordService.updateHaoqiCoinRecord(haoqiCoinRecord);
        //金额返回给用户
        Member member = this.memberService.selectMemberById(oldRec.getMemberId());

        Member update = new Member();
        update.setId(member.getId());
        update.setHaoqiCoin(member.getHaoqiCoin().add(oldRec.getNum()));
        this.memberService.updateMember(update);
        return AjaxResult.success();
    }

}
